<template>
  <div class="app-container">
  <div class="filter-container">
        <el-button type="primary" @click="addClick()">新增采购单</el-button>
        <el-input clearable v-model="search.number" placeholder="订单编号" style="width: 170px;margin-left:10px;"></el-input>
        <el-input clearable v-model="search.name" placeholder="支付通知单号" style="width: 170px;margin-left:10px;"></el-input>
        <el-date-picker v-model="search.daterange" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" style="margin-left:10px;">
        </el-date-picker>

        <el-button type="primary" icon="el-icon-search" @click="searchClick()" style="margin-left:10px;">查询</el-button>
    </div>
    <el-table v-loading="listLoading" :data="list" 
      border fit highlight-current-row stripe 
      style="width: 100%">
      <el-table-column align="center" label="序" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row._id }}</span>
        </template>
      </el-table-column>
     <el-table-column  min-width="100" align="center" label="采购订单编号">
        <template slot-scope="scope">
          <span>{{ scope.row.ordernumber }}</span>
        </template>
      </el-table-column>
      <el-table-column  min-width="100" align="center" label="支付通知号">
        <template slot-scope="scope">
          <span>{{ scope.row.paynumber }}</span>
        </template>
      </el-table-column>
      <el-table-column  min-width="100" align="center" label="具体车型">
        <template slot-scope="scope">
          <span>{{ scope.row.carname }}</span>
        </template>
      </el-table-column>
      
      <el-table-column  min-width="100" align="center" label="价税合计">
        <template slot-scope="scope">
          <span>{{ scope.row.priceandtax }}</span>
        </template>
      </el-table-column>
      <el-table-column  min-width="100" align="center" label="采购渠道">
        <template slot-scope="scope">
          <span>{{ scope.row.purchasetype }}</span>
        </template>
      </el-table-column>
      <el-table-column  min-width="100" align="center" label="供应商名称">
        <template slot-scope="scope">
          <span>{{ scope.row.supplier }}</span>
        </template>
      </el-table-column>
      <el-table-column  min-width="100" align="center" label="记录日期">
        <template slot-scope="scope">
          <span>{{ scope.row.createtime }}</span>
        </template>
      </el-table-column>
      <el-table-column  min-width="100" align="center" label="付款阶段">
        <template slot-scope="scope">
          <span>{{ scope.row.paystage }}</span>
        </template>
      </el-table-column>
      <el-table-column  min-width="100" align="center" label="付款状态">
        <template slot-scope="scope">
          <span>{{ scope.row.paystate }}</span>
        </template>
      </el-table-column>
      <el-table-column  min-width="100" align="center" label="入库状态">
        <template slot-scope="scope">
          <span>{{ scope.row.storestate }}</span>
        </template>
      </el-table-column>
      <el-table-column  min-width="100" align="center" label="制单人">
        <template slot-scope="scope">
          <span>{{ scope.row.guider }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作"  width="320" fixed="right">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="tableRowClick().entry(scope.row.id)">入库</el-button>
          <el-button type="primary" size="small" @click="tableRowClick().edit(scope.row.id)">修改</el-button>
          <el-button type="primary" size="small" @click="tableRowClick().delete(scope.row.id)">删除</el-button>
          <el-button type="primary" size="small" @click="tableRowClick().details(scope.row.id)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.rows" @pagination="fetchData" />

<!-- 新增/编辑 -->
<el-dialog :title="dialogAdd.title" :visible.sync="dialogAdd.visible"  width="900px">
    <div class="ci">
      <div class="item">
          <span class="name">采购渠道：</span>
          <span class="value">
              <el-select v-model="dialogAdd.data.purchasetype" clearable placeholder="--" style="width: 220px;">
                    <el-option 
                    v-for="(item,index) in selectOptions.purchasetype"
                    :key="index"
                    :label="item.label"
                    :value="item.value">
                    </el-option>
                </el-select>
          </span>
      </div>
      <div class="item">
          <span class="name">付款阶段：</span>
          <span class="value">
              <el-select v-model="dialogAdd.data.paystage" clearable placeholder="--" style="width: 220px;">
                    <el-option 
                    v-for="(item,index) in selectOptions.paystage"
                    :key="index"
                    :label="item.label"
                    :value="item.value">
                    </el-option>
                </el-select>
          </span>
      </div>
      <div class="item">
          <span class="name">供应商：</span>
          <span class="value">
              <el-input clearable v-model="dialogAdd.data.supplier" placeholder="" style="width:220px;" ></el-input>
          </span>
      </div>
      <div class="item">
          <span class="name">采购订单编号：</span>
          <span class="value">
              <el-input clearable v-model="dialogAdd.data.purchasenumber" placeholder="" style="width:220px;" ></el-input>
          </span>
      </div>
      <div class="item">
          <span class="name">支付通知单号：</span>
          <span class="value">
              <el-input clearable v-model="dialogAdd.data.paynumber" placeholder="" style="width:220px;" ></el-input>
          </span>
      </div>
      <div class="item">
          <span class="name">具体车型：</span>
          <span class="value">
              <el-input clearable v-model="dialogAdd.data.carname" placeholder="" style="width:220px;" ></el-input>
          </span>
      </div>
      <div class="item">
          <span class="name">价税合计</span>
          <span class="value">
              <el-input clearable v-model="dialogAdd.data.priceandtax" placeholder="" style="width:220px;" ></el-input>
          </span>
      </div>
      <div class="item hauto">
          <span class="name">备注</span>
          <span class="value">
              <el-input type="textarea" :rows="3" clearable v-model="dialogAdd.data.remark" placeholder="" style="width:100%" ></el-input>
          </span>
      </div>
      
    </div>
    <div slot="footer" class="dialog-footer">
        <el-button @click="dialogAdd.visible = false">取 消</el-button>
        <el-button type="primary" @click="dialogAddClick().save()" :loading="dialogAdd.saveloading">保 存</el-button>
    </div>
</el-dialog>
<!-- end  新增/编辑 -->

<!-- 入库 -->
<el-dialog :title="dialogEntry.title" :visible.sync="dialogEntry.visible"  width="900px">
    <div class="ci">
      
      <div class="item">
          <span class="name">仓库名称：</span>
          <span class="value">
              <el-input clearable v-model="dialogEntry.data.storename" placeholder="" style="width:220px;" ></el-input>
          </span>
      </div>
      <div class="item">
          <span class="name">仓位名称：</span>
          <span class="value">
              <el-input clearable v-model="dialogEntry.data.storeplace" placeholder="" style="width:220px;" ></el-input>
          </span>
      </div>
      <div class="item">
          <span class="name">VIN码：</span>
          <span class="value">
              <el-input clearable v-model="dialogEntry.data.vincode" placeholder="" style="width:220px;" ></el-input>
          </span>
      </div>
      <div class="item">
          <span class="name">发动机号：</span>
          <span class="value">
              <el-input clearable v-model="dialogEntry.data.enginenumber" placeholder="" style="width:220px;" ></el-input>
          </span>
      </div>
      <div class="item">
          <span class="name">运费：</span>
          <span class="value">
              <el-input clearable v-model="dialogEntry.data.transitfee" placeholder="" style="width:220px;" ></el-input>
          </span>
      </div>
      <div class="item hauto">
          <span class="name">备注</span>
          <span class="value">
              <el-input type="textarea" :rows="3" clearable v-model="dialogEntry.data.remark" placeholder="" style="width:100%" ></el-input>
          </span>
      </div>
      
    </div>
    <div slot="footer" class="dialog-footer">
        <el-button @click="dialogEntry.visible = false">取 消</el-button>
        <el-button type="primary" @click="dialogEntryClick().save()" :loading="dialogEntry.saveloading">保 存</el-button>
    </div>
</el-dialog>
<!-- end  入库 -->

  </div>
</template>

<script>
import * as carPurchaseApi from '@/api/cars/purchase'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import * as enumHelper from '@/utils/enum'
import {objectMerge,objectEmpty} from '@/utils/index'

export default {
    name:'carlist2',
    components: { Pagination },
    data(){
        return{
          search:{ 
              number:'',
              customer:'',
              phone:'',
              daterange:'',
          },
          listQuery: {
                page: 1,
                rows: 20,
                name:'',
                phone:'',
                state:-1,
                daterange:''
          },
          list:null,
          listLoading:false,
          total:0,
          selectOptions:{
            purchasetype:[],
            paystage:[],
          },

          dialogAdd:{
              visible:false,
              saveloading:false,
              title:'新增/编辑',
              data:{
                  id:'',
                  purchasetype:'',
                  paystage:'',
                  supplier:'',
                  purchasenumber:'',
                  paynumber:'',
                  carname:'',
                  priceandtax:'',
                  remark:'',
              }
          },
          dialogEntry:{
              visible:false,
              saveloading:false,
              title:'入库',
              data:{
                  id:'',
                  purchasetype:'',
                  paystage:'',
                  supplier:'',
                  purchasenumber:'',
                  paynumber:'',
                  carname:'',
                  priceandtax:'',
                  remark:'',
              }
          },
          
        }
    },
    created(){
        var that=this;
        //初始化下拉菜单
        enumHelper.initOptions(that.selectOptions.purchasetype,enumHelper.cars.purchaseType);
        enumHelper.initOptions(that.selectOptions.paystage,enumHelper.cars.payStage);
        that.fetchData();
    },
    filters:{

      parseSalesState(val){
        return enumHelper.presale.salesState.getName(val);
      }

    },
    methods:{
        fetchData(){
          var that=this;
            carPurchaseApi.getPurchaseList({}).then(res=>{
              if(res.data){
                that.list=[...res.data.items.map((item,index)=>{
                  item._id=index+1;
                  return item;
                })];
              }
            })
        },
        addClick(){
          this.dialogAdd.visible=true;
          this.dialogAddClick().init(null);
        },
        tableRowClick(){
          var that=this;
          return {
            edit(id){
              var data=that.list.filter(o=>id==id)[0];
              that.dialogAddClick().init(data);
              that.dialogAdd.visible=true;
            },
            delete(id){
              that.$confirm('确定删除这条记录码?', '提示', {
                confirmButtonText: '确定', cancelButtonText: '取消',type: 'warning'}).then(() => {
                  that.$message({
                    type: 'success',
                    message: '删除成功!'
                  });
                  }).catch(() => {
                    that.$message({
                      type: 'info',
                      message: '已取消删除'
                    });          
                  });
            },
            entry(id){
              that.dialogEntryClick().init();
              that.dialogEntry.visible=true;
            },
            details(id){

            }
          }
        },
        dialogAddClick(){
          var that=this;
          var dg=that.dialogAdd;
          return{
            init(data){
              data?objectMerge(dg.data,data):objectEmpty(dg.data);
            },
            save(){
              dg.visible=false;
            }
          }
        },
        dialogEntryClick(){
          var that=this;
          var dg=that.dialogEntry;
          return{
            init(){
              objectEmpty(dg.data);
            },
            save(){
              dg.visible=false;
            }
          }
        }
    }
}
</script>

<style scoped>

</style>